﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace Bingootest1
{
    public partial class Game : PhoneApplicationPage
    {

        private System.Windows.Threading.DispatcherTimer myDispatcherTimer;
        private int SecondsLeft = 15;

        private bool IsWin = false;
        public Game()
        {
            InitializeComponent();
            button1.Content = ((App)Application.Current).words[0];
            button2.Content = ((App)Application.Current).words[1];
            button3.Content = ((App)Application.Current).words[2];
            button4.Content = ((App)Application.Current).words[3];
            GameCount = 4;

            String s = (SecondsLeft % 60).ToString();
            if (s.Length == 1) s = "0" + s;
            textBlock1.Text = "Time left: " + (SecondsLeft / 60).ToString() + ":" + s;


            myDispatcherTimer = new System.Windows.Threading.DispatcherTimer();
            myDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000); // 100 Milliseconds
            myDispatcherTimer.Tick += new EventHandler(Each_Tick);
            myDispatcherTimer.Start();
        }

        private int GameCount = 4;

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            ((Button)sender).IsEnabled = false;
            GameCount--;
            if (GameCount == 0)
            {
                IsWin = true;
                storyboard1.Completed += new EventHandler(MyStoryboard_Completed);
                storyboard1.Begin();
                SecondsLeft = -1;
            }
        }





        /*
             * Add this function to your code file
             */
        void MyStoryboard_Completed(object sender, EventArgs e)
        {
            if (IsWin)
            {
                NavigationService.Navigate(new Uri("/GameOverPage.xaml", UriKind.RelativeOrAbsolute));
            }
            else
            {
                NavigationService.Navigate(new Uri("/Lose.xaml", UriKind.RelativeOrAbsolute));
            }
        }

        public void Each_Tick(object o, EventArgs sender)
        {
            SecondsLeft--;
            String s = (SecondsLeft %60).ToString();
            if (s.Length == 1) s = "0" + s;
            textBlock1.Text = "Time left: " + (SecondsLeft / 60).ToString() + ":" + s;
            if (SecondsLeft == 0)
            {
                storyboard1.Completed += new EventHandler(MyStoryboard_Completed);
                storyboard1.Begin();
                
            }
        }

    }
}